<?php
// 處室資料
class Default_Model_Upgrade_TeacherTitle extends Default_Model_Upgrade_Core
{
    protected $_table = 'teacher_title';
    protected $_targetTable = array('teacher_title');

    function upgrade (){
        if ($this->isUpgrade())
        return 'isUpgrde';
        else {
            $query = "SELECT * FROM teacher_title";

            $res = $this->_sfs3Pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
            $log = array();
            $success_count = 0;
            $error_count = 0;
            $sort= 0;

            foreach($res as $row){
                $arr = array();
                $role_arr = array();
                $arr['name'] = $this->iconv($row['title_name']);
                $arr['sn'] = $row['teach_title_id'];
                $arr['kind_sn'] =$row['title_kind'];
                $arr['division_sn'] = $row['room_id'];
                $arr['duty'] = $this->iconv( $row['title_memo']);

                $arr['name_short'] = $this->iconv($row['title_short_name']);
                $arr['enable'] = ($row['enable']==1)?'Y':'N';
                $arr['sort'] = ++$sort;
                // 處室與職稱重複處理
                $find = $this->_newdbPdo->query("SELECT name FROM school_room
				      WHERE name='{$arr['name']}'")->fetch();
                if ($find)
                $arr['name'] = $arr['name'].'(職稱)';

                if (!$this->fromArray('teacher_title',$arr)) {
                     //print_r($arr); exit;
                    $log['error_row'][] = $row['teach_title_id'];
                    $error_count++;
                }
                else {
                    // 加入 acl_roles
                    $arr2 = array();
                    $arr2['id'] = $arr['name'];
                    $arr2['role_kind'] = 'title';
                    $this->fromArray('acl_roles',$arr2);
                }


            }
            $log['success_count'] = $success_count;
            $log['error_count'] = $error_count;
            $log['memo'] = 'SFS3 職稱資料升級';
            $log['upgrade_time']	= date("Y-m-d h:i:s");
            $this->finishUpgrade($log);
        }
    }

}